package main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;

import settings.Settings;

/**
 * The splashscreen is a little window which can display a logo and 
 * has methods to set the progress (as text and bar)
 * @author daishy
 * @date 02.04.2008 15:37:12
 *
 */
public class Splashscreen extends JWindow
{  
	/**
	 * Default Serial-Ver-ID 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The progressbar for indicating the progess
	 */
	private JProgressBar progressBar;

	/**
	 * This stores the path to the image
	 */
	private String image;

	/**
	 * This method initializes the Frame and all needed 
	 * Panels and boxes.
	 */
	public Splashscreen()
	{
		this.image = new String("./src/splashimage.png");
		
		// Loading image and determining size
		int width = 0;
		int height = 0;
		ImageIcon imageIcon = new ImageIcon(this.image);
		if(imageIcon.getImageLoadStatus() == java.awt.MediaTracker.COMPLETE)
		{
			width = imageIcon.getIconWidth();
			height = imageIcon.getIconHeight();
		}
				
		// setting up bottompanel
		JPanel bottom = new JPanel(new FlowLayout());
		bottom.setBackground(Color.WHITE);

		// add progressbar
		this.progressBar = new JProgressBar();
		progressBar.setPreferredSize(new Dimension(width, 30));
		bottom.add(progressBar, null);

		//setting up Image
		// (If the file is not valid, no image is displayed)
		JLabel image = new JLabel();
		image.setIcon(imageIcon);
		image.setSize(width, height);
		

		// settingup mainpanel
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(image, BorderLayout.CENTER);
		getContentPane().add(bottom, BorderLayout.SOUTH);
		setSize(width, height + 30);
		setLocationRelativeTo(null);		
	}

	/**
	 * This method shows the splashscreen. So after you
	 * created the splashscreen, calling this method will show it
	 */
	public void display(boolean show)
	{
		if(show){
			setVisible(true);
		}
		else {
			setVisible(false);
			dispose();
		}
	}

	/**
	 * Sets the maximal progress.
	 * @param maxProgress
	 */
	public void setProgressMax(int maxProgress)
	{
		progressBar.setMaximum(maxProgress);
	}

	/**
	 * The method sets the progress to the specified level.
	 * This method will set the message to null, so no Method will be 
	 * displayed
	 * 
	 * @param progress 
	 * @throws IllegalArgumentException if progress is greater than maxProgress (or <0)
	 */
	public void setProgress(int progress)
	{
		setProgress(progress, null);
	}

	/**
	 * This method sets the progress to the specified level
	 * and also sets the Message will be displayed. The message may
	 * be null, then no message is displayed
	 * @param progress The progress (between 0 and maxProgress)
	 * @param message The Message to display
	 */
	public void setProgress(int progress, String message)
	{
		if(progress < 0 || progress > progressBar.getMaximum())
			throw new IllegalArgumentException("progress (" + progress + ") not in bounds");

		// set the progress
		this.progressBar.setValue(progress);

		// Set the message
		if (message==null) {
			progressBar.setStringPainted(false);
			progressBar.setString("");
		}
		else {
			progressBar.setStringPainted(true);
			progressBar.setString(message);
		}
	}
	
	private void getSettings()
	{
		Settings tree = Settings.getInstance().getTree("splashscreen");
		
		tree.addDefaultValue("image", "./src/splashimage.png");	
		
		
		
		
	}
}
